/**
 * 快递员 - 新增或编辑界面
 */
Ext.define("PSI.Express.ExpressMan.EditForm", {
			extend : "Ext.window.Window",
			config : {
				parentForm : null,
				entity : null
			},

			initComponent : function() {
				var me = this;
				var entity = me.getEntity();
				me.adding = entity == null;
				var buttons = [];
				if (!entity) {
					buttons.push({
								text : "保存并继续新增",
								formBind : true,
								handler : function() {
									var me = this;
									me.onOK(true);
								},
								scope : this
							});
				}

				buttons.push({
							text : "保存",
							formBind : true,
							iconCls : "PSI-button-ok",
							handler : function() {
								me.onOK(false);
							},
							scope : me
						}, {
							text : me.adding ? "关闭" : "取消",
							handler : function() {
								me.close();
							},
							scope : me
						});

				Ext.apply(me, {
							title : entity == null ? "新增快递员" : "编辑快递员",
							modal : true,
							resizable : false,
							onEsc : Ext.emptyFn,
							width : 520,
							height : 180,
							layout : "fit",
							items : [{
								id : "editForm",
								xtype : "form",
								layout : {
									type : "table",
									columns : 2
								},
								height : "100%",
								bodyPadding : 5,
								defaultType : 'textfield',
								fieldDefaults : {
									labelWidth : 90,
									labelAlign : "right",
									labelSeparator : "",
									msgTarget : 'side'
								},
								items : [{
									xtype : "hidden",
									name : "id",
									value : entity == null ? null : entity
											.get("id")
								}, {
									id : "editName",
									fieldLabel : "姓名",
									allowBlank : false,
									blankText : "没有输入姓名",
									beforeLabelTextTpl : PSI.Const.REQUIRED,
									name : "name",
									value : entity == null ? null : entity
											.get("name"),
									listeners : {
										specialkey : {
											fn : me.onEditSpecialKey,
											scope : me
										}
									},
									width : 490,
									colspan : 2
								}, {
									id : "editMobile",
									fieldLabel : "手机号",
									allowBlank : false,
									blankText : "没有输入手机号",
									beforeLabelTextTpl : PSI.Const.REQUIRED,
									name : "mobile",
									value : entity == null ? null : entity
											.get("mobile"),
									listeners : {
										specialkey : {
											fn : me.onEditSpecialKey,
											scope : me
										}
									},
									width : 490,
									colspan : 2
								}, {
									id : "editBindCode",
									fieldLabel : "绑定微信校验码",
									name : "bindCode",
									value : entity == null ? null : entity
											.get("bindCode"),
									listeners : {
										specialkey : {
											fn : me.onEditLastSpecialKey,
											scope : me
										}
									},
									width : 490,
									colspan : 2
								}, {
									xtype : "displayfield",
									fieldLabel : "备注",
									value : "如果验证码不填写，后台就自动把手机号作为校验码"
								}],
								buttons : buttons
							}],
							listeners : {
								show : {
									fn : me.onWndShow,
									scope : me
								},
								close : {
									fn : me.onWndClose,
									scope : me
								}
							}
						});

				me.callParent(arguments);

				me.editName = Ext.getCmp("editName");
				me.editMobile = Ext.getCmp("editMobile");
				me.editBindCode = Ext.getCmp("editBindCode");

				me.editForm = Ext.getCmp("editForm");

				me.__editorList = [me.editName, me.editMobile, me.editBindCode];
			},

			onWndShow : function() {
				var me = this;

				var edit = me.editName;
				edit.focus();
				edit.setValue(edit.getValue());
			},

			onWndClose : function() {
				var me = this;
				if (me.__lastId) {
					if (me.getParentForm()) {
						me.getParentForm().onQuery();
					}
				}
			},

			onOK : function(thenAdd) {
				var me = this;
				var f = me.editForm;
				var el = f.getEl();
				el.mask(PSI.Const.SAVING);
				f.submit({
							url : PSI.Const.BASE_URL
									+ "Home/Express/editExpressMan",
							method : "POST",
							success : function(form, action) {
								el.unmask();
								me.__lastId = action.result.id;
								PSI.MsgBox.tip("数据保存成功");
								me.focus();
								if (thenAdd) {
									me.clearEdit();
								} else {
									me.close();
								}
							},
							failure : function(form, action) {
								el.unmask();
								PSI.MsgBox.showInfo(action.result.msg,
										function() {
											me.editName.focus();
										});
							}
						});
			},

			onEditSpecialKey : function(field, e) {
				var me = this;

				if (e.getKey() === e.ENTER) {
					var id = field.getId();
					for (var i = 0; i < me.__editorList.length; i++) {
						var editorId = me.__editorList[i].getId();
						if (id === editorId) {
							var edit = me.__editorList[i + 1];
							edit.focus();
							edit.setValue(edit.getValue());
						}
					}
				}
			},

			onEditLastSpecialKey : function(field, e) {
				var me = this;
				if (e.getKey() == e.ENTER) {
					if (me.editForm.getForm().isValid()) {
						me.onOK(me.adding);
					}
				}
			},

			clearEdit : function() {
				var me = this;

				me.editName.focus();

				var editors = [me.editName, me.editMobile];
				for (var i = 0; i < editors.length; i++) {
					var edit = editors[i];
					if (edit) {
						edit.setValue(null);
						edit.clearInvalid();
					}
				}
			}
		});